//
//  YZYNetworkTools.swift
//  weibo
//
//  Created by mac on 16/10/27.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import AFNetworking
// get post 请求方式枚举
enum YZYNetworkToolsMethod: String {
    case get = "get"
    case post = "post"
}

class YZYNetworkTools: AFHTTPSessionManager {
    // 全局访问点
    static let sharedTools: YZYNetworkTools = {
        let tools = YZYNetworkTools()
        // 反序列化
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    /// 网络请求的公共方法
    ///
    /// - parameter method:     请求方式
    /// - parameter urlString:  url
    /// - parameter parameters: 请求参数
    /// - parameter success:    成功
    /// - parameter failure:    失败
    func requet(method: YZYNetworkToolsMethod, urlString: String, parameters: Any?, success:@escaping (Any?)->(), failure:@escaping (Error)->()) {
        // 如果是get请求
        if method == .get {
            get(urlString, parameters: parameters, progress: nil, success: { (_, responseObject) in
                success(responseObject)
                }, failure: { (_, error) in
                    failure(error)
            })
        } else {
            // post 请求
            post(urlString, parameters: parameters, progress: nil, success: { (_, responseObject) in
                success(responseObject)
                }, failure: { (_, error) in
                    failure(error)
            })
        }
    }

}

// MARK: - 首页相关接口
extension YZYNetworkTools {
    func homeLoadData(sinceId: Int64, maxId: Int64, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        // 请求 url
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        // 请求参数
        let params: [String: Any] = [
            "access_token": YZYUserAccountViewModel.sharedTools.accessToken!,
            "since_id":sinceId,
            "max_id":maxId
        ]
        
        // 发送请求
        requet(method: YZYNetworkToolsMethod.get, urlString: urlString, parameters: params, success: success, failure: failure)
    }
}


// MARK: - OAuth 授权相关接口
extension YZYNetworkTools {
    // 获取 userAccount
    func oauthLoadUserAccount(code: String, success:@escaping (Any?)->(), failure:@escaping (Error)->()) {
        // 请求 urlStrign
        let urlString = "https://api.weibo.com/oauth2/access_token"
        // 请求参数
        let params = [
            "client_id": YZYAPPKEY,
            "client_secret": YZYAPPSECRET,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": YZYREDIRECTURI
        ]
        // 发送请求
        requet(method: YZYNetworkToolsMethod.post, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    
    // 请求用户信息
    func oauthLoadUserInfo(model: YZYUserAccountModel, success:@escaping (Any?)->(), failure:@escaping (Error)->()) {
        // 请求urlString
        let utlString = "https://api.weibo.com/2/users/show.json"
        // 请求参数
        let params = [
            "access_token":model.access_token!,
            "uid":model.uid!
        ]
        // 发送请求
        requet(method: YZYNetworkToolsMethod.get, urlString: utlString, parameters: params, success: success, failure: failure)
    }
}
